/**
 *
 */
package anysware.source.resource;

import org.apache.commons.lang.StringUtils;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Status;
import org.restlet.resource.Representation;
import org.restlet.resource.ResourceException;
import org.restlet.resource.Variant;

import anysware.core.rest.resource.AnyswareResource;
import anysware.restlet.IllegalRepresentationException;
import anysware.source.Source;
import anysware.source.SourceService;

/**
 * TODO: doc me
 *
 * <table>
 * <tr>
 * <th>Method</th>
 * <th>URL Template</th>
 * </tr>
 * <tr>
 * <td>GET, PUT</td>
 * <td><code>/source/{name}.{type}</code></td>
 * </tr>
 * <tr>
 * <td>DELETE</td>
 * <td><code>/source/{name}</code></td>
 * </tr>
 * <tr>
 * <td>POST</td>
 * <td><code>/source</code></td>
 * </tr>
 * </table>
 *
 *
 * @author keke<keke@codehaus.org>
 * @revision $Revision: 111 $
 * @version
 */
public class SourceResource extends AnyswareResource {
	private boolean	         allowPost	= false;
	private transient Source	source;
	private SourceService	 sourceService;

	/*
	 * (non-Javadoc)
	 *
	 * @see org.restlet.resource.Resource#acceptRepresentation(org.restlet.resource.Representation)
	 */
	@Override
	public void acceptRepresentation(final Representation entity)
	        throws ResourceException {
		try {
			final Source source = (Source) repHelper.toEntity("source", entity);
			sourceService.save(source);
			getResponse().setStatus(Status.SUCCESS_CREATED);
			getResponse().setEntity(
			        repHelper.toRepresentation("source", source, new Variant(
			                MediaType.APPLICATION_JSON)));
		} catch (final IllegalRepresentationException e) {
			getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.restlet.resource.Resource#allowPost()
	 */
	@Override
	public boolean allowPost() {
		return allowPost;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.restlet.resource.Resource#removeRepresentations()
	 */
	@Override
	public void removeRepresentations() throws ResourceException {
		sourceService.delete(source);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.restlet.resource.Resource#represent(org.restlet.resource.Variant)
	 */
	@Override
	public Representation represent(final Variant variant)
	        throws ResourceException {
		final Representation rep = repHelper.toRepresentation("source", source,
		        variant);
		if (rep == null) {
			getResponse().setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
		}
		return rep;
	}

	/**
	 * @param sourceService
	 *            the sourceService to set
	 */
	public void setSourceService(final SourceService sourceService) {
		this.sourceService = sourceService;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.restlet.resource.Resource#storeRepresentation(org.restlet.resource.Representation)
	 */
	@Override
	public void storeRepresentation(final Representation entity)
	        throws ResourceException {
		sourceService.save((Source) repHelper.toEntity("source", entity));
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.restlet.resource.Resource#init(org.restlet.Context,
	 *      org.restlet.data.Request, org.restlet.data.Response)
	 */
	@Override
	protected void initInner() {

		final String sourceName = (String) getRequest().getAttributes().get(
		        "name");
		if (StringUtils.isEmpty(sourceName)) {
			if (!getRequest().getMethod().equals(Method.POST)) {
				getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
			} else {
				allowPost = true;
				setModifiable(true);
			}
		} else {
			source = sourceService.getSource(sourceName);
			if (source == null) {
				getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
			}
			setAvailable(source != null);
			setModifiable(isAvailable());
		}
	}

}
